package net.cyl.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import lombok.extern.slf4j.Slf4j;
import net.cyl.component.SmsComponent;
import net.cyl.config.SmsConfig;
import net.cyl.constant.RedisKey;
import net.cyl.emums.BizCodeEnum;
import net.cyl.emums.SendCodeEnum;
import net.cyl.service.NotifyService;
import net.cyl.util.CheckUtil;
import net.cyl.util.CommonUtil;
import net.cyl.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class NotifyServiceImpl implements NotifyService {
    //验证码过期时间
    private static final int CODE_EXPRIED_TIME = 60 * 1000*10;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private SmsComponent smsComponent;
    @Autowired
    private SmsConfig smsConfig;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public JsonData sendSms(SendCodeEnum sendCodeEnum, String to) {
        //生成缓存key
        String cacheKey = String.format(RedisKey.CHECK_CODE_KEY,sendCodeEnum.name(),to);
        //获取缓存value
        String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey);
        //如果缓存value不为空
        if(StringUtils.isNotBlank(cacheValue)){
            //获取缓存过期时间
            long ttl = Long.parseLong(cacheValue.split("_")[1]);
            //获取当前时间戳
            long leftTime = CommonUtil.getCurrentTimestamp() - ttl;
            //如果验证码未过期
            if(leftTime < (60 * 1000)){
                //打印日志
                log.info("验证码未过期，请稍后再试{}",leftTime);
                //返回错误信息
                return JsonData.buildResult(BizCodeEnum.CODE_LIMITED);
            }
        }
        //生成随机验证码
        String code = CommonUtil.getRandomCode(4);
        //生成缓存value
        String value = code + "_"+CommonUtil.getCurrentTimestamp();
        //设置缓存
        stringRedisTemplate.opsForValue().set(cacheKey,value,CODE_EXPRIED_TIME, TimeUnit.MILLISECONDS);
        //判断to是否为手机号
        if(CheckUtil.isPhone(to)){
            //发送短信
            smsComponent.sendSms("code:"+code,smsConfig.getTemplateId(),to);
        }else if(CheckUtil.isEmail(to)){
            //发送邮件 TODO
        }
        //返回成功信息
        return JsonData.buildSuccess();
    }

    @Override
    public boolean checkCode(SendCodeEnum sendCodeEnum, String to, String code) {
        //根据sendCodeEnum和to生成缓存key
        String cacheKey = String.format(RedisKey.CHECK_CODE_KEY,sendCodeEnum.name(),to);
        //从缓存中获取value
        String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey);
        //如果缓存中有值
        if(StringUtils.isNotBlank(cacheValue)){
            //获取value中的第一个值
            String value = cacheValue.split("_")[0];
            //如果value和code相等
            if(value.equalsIgnoreCase(code)){
                //删除缓存
                stringRedisTemplate.delete(cacheKey);
                return true;
            }
        }
        return false;
    }
}
